package com.xiaobai.easyexcel;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.xiaobai.java_core.string_practice.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangtw
 * @ClassName CustomCellWriterHandle
 * @description: 自定义EasyExcel拦截器
 * @date 2023/11/1222:31
 */
@Slf4j
public class CustomCellWriterHandle implements CellWriteHandler {

    private Map<String, String> dynamicMap = new HashMap<>();

    public CustomCellWriterHandle(Map<String, String> dynamicMap) {
        this.dynamicMap = dynamicMap;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder,
                                 WriteTableHolder writeTableHolder,
                                 List<CellData> list, Cell cell,
                                 Head head, Integer integer,
                                 Boolean isHead) {
        if (isHead) {
            String headName = cell.getStringCellValue();
            log.info("headName: {}", headName);
            headName = StringUtil.format(headName, dynamicMap);
            cell.setCellValue(headName);
        }
    }
}
